// JavaScript Document
define(function(require, exports, module) {
	var Masker = require("Masker");

	var FrameWindow = function() {
	}
	FrameWindow.left = 50
	FrameWindow.top = 50
	FrameWindow.zIndex = 5000;
	FrameWindow.prototype.left = 50;
	FrameWindow.prototype.top = 50;
	FrameWindow.prototype.opened = false;
	FrameWindow.prototype.sizeType = "";
	FrameWindow.prototype.id = "";
	FrameWindow.prototype.create = function() {
		var self = this;
		this.element = document.createElement("div");
		this.element.className = "ht_window " + this.sizeType + " close";
		//this.element.style.zIndex = FrameWindow.zIndex;

		this.left = FrameWindow.left
		this.top = FrameWindow.top

		if (this.sizeType != "fullScreen") {
			this.element.style.left = this.left + "%";
			this.element.style.top = this.top + "%";
		}
		if (this.id != "") {
			this.element.id = this.id+"FrameWindow";
		}
		FrameWindow.zIndex++
		this.element.style.zIndex = FrameWindow.zIndex;
		
		
		
		
		this.wrapper = document.createElement("div");
		this.wrapper.className = "wrapper"
		this.title = document.createElement("h4");
		this.title.className = "title"
		this.title.innerHTML = ""

		this.closeButton = document.createElement("input")
		this.closeButton.type = "button"
		this.closeButton.className = "closeButton"
		this.closeButton.value = "关闭"
		if((typeof self.obj != "undefined" && self.obj != null)){
			this.wrapper.className = "wrapper confirmModal"
			this.confrimModuel=document.createElement("div");
			this.message=document.createElement("p");
			this.message.innerText=this.obj.confirmMsg
			this.message.style.margin="20px";
			this.yesBtn=document.createElement("a");
			this.yesBtn.className="btn btn-primary btn-md";
			$(this.yesBtn).css({"position":"absolute","bottom":"7%","left":"9rem"})
			this.yesBtn.innerHTML=this.obj.yesStr;
			this.yesBtn.onclick=function(){
				self.obj.ele.get(0).click();
				self.close();
			};
			this.noBtn=document.createElement("a");
			this.noBtn.className="btn btn-primary btn-md";
			this.noBtn.innerHTML=this.obj.noStr;
			$(this.noBtn).css({"position":"absolute","bottom":"7%","right":"8rem"})
			this.noBtn.onclick=function(){
				self.close();
			}
			this.confrimModuel.appendChild(this.message)
			this.confrimModuel.appendChild(this.yesBtn);
			this.confrimModuel.appendChild(this.noBtn);
		}else{
			this.iframe = document.createElement("iframe")
			this.iframe.frameBorder = 0;
			this.iframe.scrolling = "yes";
		}





		this.element.appendChild(this.wrapper)
		this.wrapper.appendChild(this.title)
		this.wrapper.appendChild(this.closeButton)
		if((typeof self.obj != "undefined" && self.obj != null)){
			this.wrapper.appendChild(this.confrimModuel);
		}
		else{
			this.wrapper.appendChild(this.iframe)
		}

		this.mouseMoveActionHelper=function(e){
			var e=e||window.event;
			self.wrapper.style.left=self.recordX+(e.clientX-self.mouseStartX)+"px";
			self.wrapper.style.top=self.recordY+(e.clientY-self.mouseStartY)+"px";
		}
		this.mouseMoveAction=function(e){
			$(self.title).bind("mousedown",function(e){
				var e=e||window.event;
				self.recordX=$(self.wrapper).position().left;
				self.recordY=$(self.wrapper).position().top;
				self.mouseStartX=e.clientX;
				self.mouseStartY=e.clientY;
				$(self.wrapper).bind("mousemove",self.mouseMoveActionHelper);
				$(self.title).css("cursor","move");
			});
			$(self.wrapper).bind("mouseup",function(e){
				$(self.wrapper).unbind("mousemove",self.mouseMoveActionHelper);
				$(self.title).css("cursor","default");
			})
			$(self.wrapper).bind("mouseout",function(e){
				try{
					$(self.wrapper).unbind("mousemove",self.mouseMoveActionHelper);
					$(self.title).css("cursor","default");
				}catch(error){
					console.log(error);
				}
			})
		}
		

		if(this.sizeType != "fullScreen"){
			self.mouseMoveAction();
		}else{
			var cHeight=$(document.body).height()-20-57;
			self.iframe.style.height=cHeight+"px";
			
			$(window).bind("resize",function(event){
				var cHeight=$(document.body).height()-20-57;
				self.iframe.style.height=cHeight+"px";
				return true;
			})
		}
		document.body.appendChild(this.element)
		$(".closeButton", this.element).bind("click", function(event) {
			self.closeButtonClickHandler(event)
		})
		$(this.element).bind("click", function(event) {
			self.clickHandle(event)
		})
	}
	FrameWindow.prototype.clickHandle = function(event) {
		this.zIndexUp();
	}

	FrameWindow.prototype.closeButtonClickHandler = function(event) {
		this.close();
	}
	

	// z轴上升
	FrameWindow.prototype.zIndexUp = function() {
		FrameWindow.zIndex++
		this.element.style.zIndex = FrameWindow.zIndex;
	}
	//重新加载
	FrameWindow.prototype.reload = function() {
		try{
			this.iframe.contentWindow.location.reload();
		}catch(e){
			console.log(e);
		}
	}
	// 关闭
	FrameWindow.prototype.close = function() {
		var self=this;
		clearInterval(this.updateTitleTimer);

		if (this.opened == false) {
			return false;
		}
		if(self.obj!=null&&!!self.obj.ele.attr("confirmAction")&&self.obj.ele.data("signVal")=="false"){
			self.obj.ele.data("signVal","true");
		}
		this.element.className = "ht_window " + this.sizeType + " close";
		this.element.style.top = (this.top + 2) + "%";
		this.masker.close()
		var self = this;
		// self.remove();
		var timer = setTimeout(function() {
			self.remove();
		}, 500);
		//this.wrapper.removeChild(this.iframe)
		// $(this.element).animate({opacity:'0'},"fast");
		// document.body.removeChild(this.element)
		this.opened = false;

		if (FrameWindow.left > 50) {
			FrameWindow.left -= 1
			FrameWindow.top -= 3
			//FrameWindow.zIndex++;
		} else {
			FrameWindow.left = 50
			FrameWindow.top = 50
		}
		try {
			top.content.baseContent.focus();
		} catch (error) {

		}
	}

	// 打开
	FrameWindow.prototype.open = function(titleStr, src, launchParentWindow,
			sizeType,id,obj) {
		//因为a title设置不准确不可开启
		titleStr="";
		this.titleStr=titleStr;
		if (typeof (sizeType) != "undefined") {
			this.sizeType = sizeType;
		}
		if (typeof id != "undefined" && id != null) {
			this.id = id;
		}
		if (src == "" || typeof (src) == "undefined" || src == null) {
			return false;
		}
		if (src.indexOf("%7B") != "-1") {
			return false;
		}

		if (typeof obj != "undefined" && obj != null) {
			this.obj = obj;
			this.titleStr=this.obj.titleStr;
		}
		var self = this;
		this.masker = new Masker();
		this.masker.open()

		this.create();
		this.title.innerHTML = "";
		
		if(this.titleStr!="" && this.titleStr!=null){
			this.title.innerHTML = this.titleStr;
		}
		
		this.launchParentWindow = launchParentWindow;
		var self = this;
		setTimeout(function() {
			self.element.className = "ht_window " + self.sizeType + " open"
			if (self.sizeType != "fullScreen") {
				self.element.style.top = self.top + "%";
			}
			//$(self.element).addClass("ht-window-fullScreen");
			self.opened = true;
		}, 0)
		setTimeout(function() {
			if (src.indexOf("?") == -1) {
				src += "?"
			}
			src += "&_=" + Math.round(Math.random() * 1000);
			self.iframe.src = src
			// self.connectTimer=setInterval(function(){self.connectIFrame()},500);
		}, 200)

		this.updateTitleTimer = setInterval(function() {
			self.updateTitle();
		}, 1000)

		if (FrameWindow.left < 55) {
			FrameWindow.left += 1
			FrameWindow.top += 3
		} else {
			FrameWindow.left = 50
			FrameWindow.top = 50
		}

		return this;
	}

	// 隐藏
	
	FrameWindow.prototype.hide=function(){
		$(this.element).hide();
		this.masker.close()
	}
	

	// 更新标题
	FrameWindow.prototype.updateTitle = function() {
		if(this.titleStr=="" || this.titleStr==null){
			if (this.iframe.contentWindow.document.body != null) {
				this.title.innerHTML = this.iframe.contentWindow.document.title
			}
		}
	}

	// 连接iframe
	FrameWindow.prototype.connectIFrame = function() {
		if (this.iframe.contentWindow != null) {
			if (this.iframe.contentWindow.document.body != null) {
				this.iframe.contentWindow.launchParentWindow = this.launchParentWindow
				this.title.innerHTML = this.iframe.contentWindow.document.title
				clearInterval(this.connectTimer)
				// console.log(this.iframe.launchParentWindow)

				// $(this.element).animate({opacity:'1'},"fast");
			}
		}
	}

	// 移除
	FrameWindow.prototype.remove = function() {
		this.element.parentNode.removeChild(this.element)
		//document.body.removeChild(this.element)
	}
	module.exports = FrameWindow;
});